package com.gzc.just.play.last.war.robotclient.ai;

import com.gzc.just.play.last.war.robotclient.RobotAI;
import com.gzc.just.play.last.war.robotclient.RobotPlayer;

import java.util.Random;

/**
 * Skill Use AI
 * Corresponds to C++ version CPlayerSkillUseAI
 */
public class SkillUseAI extends RobotAI {
    private final Random random = new Random();
    
    // Skill use parameters
    private long lastSkillTime = 0;
    private static final long SKILL_INTERVAL = 5000; // 5 seconds skill interval
    
    public SkillUseAI(RobotPlayer player) {
        super(player);
    }
    
    @Override
    public int begin() {
        logInfo("Skill Use AI started");
        return 0;
    }
    
    @Override
    public int update(long currentTime) {
        if (getAIState() == AIState.RUNNING) {
            // Check if it's time to use skill
            if (currentTime - lastSkillTime > SKILL_INTERVAL) {
                // Use a random skill
                performRandomSkill();
                lastSkillTime = currentTime;
                
                // Return AI continue running
                setAIState(AIState.RUNNING);
                return 1;
            }
        }
        
        return 0;
    }
    
    @Override
    public int end() {
        logInfo("Skill Use AI ended");
        setAIState(AIState.CLEARED);
        return 0;
    }
    
    /**
     * Perform random skill usage
     */
    private void performRandomSkill() {
        // Get available skills
        var availableSkills = getPlayer().getAvailableSkills();
        
        if (availableSkills.isEmpty()) {
            logWarn("No skills available for robot {}", getPlayer().getRobotId());
            return;
        }
        
        // Select a random skill
        var skill = availableSkills.get(random.nextInt(availableSkills.size()));
        
        logInfo("Robot {} using skill {}", getPlayer().getRobotId(), skill.skillName);
        
        // Simple skill implementation
        // In a real implementation, this would send skill use message to server
        getPlayer().updateMP(getPlayer().getMp() - skill.manaCost); // Reduce MP
    }
}